#ifndef TESTSCREEN_H_20120131
#define TESTSCREEN_H_20120131

#include "cocos2d.h"

#include "Characters.h"
#include "CocosButton.h"
#include "CocosJoystick.h"
#include "CocosHelper.h"

const float BOW_BACK_SPEED = 10.0f;
class CSpriteBow : public cocos2d::CCSprite
{
public:
	CSpriteBow() 
		: m_ptUp(ccp(50, 50))
		, m_ptDown(ccp(50, 100))
		, m_ptCenter(ccp(50, 75))
	{

	}
	static CSpriteBow *spriteWithFile(const char *fileName)
	{
		CSpriteBow *popSprite = new CSpriteBow;
		if((NULL != popSprite))
		{
			popSprite->initWithFile(fileName);
			popSprite->autorelease();
			return popSprite;
		}

		CC_SAFE_DELETE(popSprite);
		return NULL;
	}

	virtual void CSpriteBow::draw()
	{
		CCSprite::draw();

		TestBack();
		glColor4f(0.0, 0.0, 0.0, 1.0);
		glLineWidth(3.0f);
		ccDrawLine(m_ptUp, m_ptCenter);
		ccDrawLine(m_ptCenter, m_ptDown);
	}

	void SetLinePara(cocos2d::CCPoint ptUp, cocos2d::CCPoint ptDown, cocos2d::CCPoint ptCenter)
	{
		m_ptUp = ptUp;
		m_ptDown = ptDown;
		m_ptCenter = ptCenter;
	}

	void Touched(cocos2d::CCPoint ptTouch)
	{
		m_bTouched = true;

	}
	void TouchedCancle()
	{
		m_bTouched = false;
	}

	void TestBack()
	{
		if(m_bTouched)
		{
			return;
		}
		
		CCPoint ptOrgCenter = ccp(0.5f * (m_ptUp.x + m_ptDown.x), 0.5f * (m_ptUp.y + m_ptDown.y));
		float len = ccpDistance(m_ptCenter, ptOrgCenter);

		if(abs(len) < BOW_BACK_SPEED)
		{
			m_ptCenter = ptOrgCenter;
			return;
		}

		CCPoint ptInc = CMathHelper::CalIncrement(m_ptCenter, ptOrgCenter, len/3);

		m_ptCenter = ccpAdd(m_ptCenter, ptInc);

	}
private:
	cocos2d::CCPoint m_ptUp;
	cocos2d::CCPoint m_ptDown;
	cocos2d::CCPoint m_ptCenter;
	bool             m_bTouched;

};

const int ID_BTN_ATTACK = 1;
class CTestScreen : public cocos2d::CCLayer, CButtonListener
{
public:
	virtual void onEnter();
	virtual void onExit();
	virtual void didAccelerate(cocos2d::CCAcceleration* pAccelerationValue);
	virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	virtual void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);

	LAYER_NODE_FUNC(CTestScreen);

	// BUTTON
	virtual void onButtonClick(int aID);

	void OnUpdate(cocos2d::ccTime dt);
private:
	void InitUI();

private:
	cocos2d::CCPoint m_ptCurAcc;
	CZhaoYun m_player;	
	CSpriteButton  *m_pBtnAttack;
	CSpriteButton  *m_pBtnTest;
	CCocosJoystick *m_pJoy;
	CLancer         m_lancer;

	CSpriteBow     *m_pSprBow;

};
#endif